package com.dx.mes.service.plan.service;
/**
 * 
 * @author: zhongXu
 * @date : 2017-6-7
 */

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_ALREADY_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FDPAPLYDN;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import com.dx.common.table.ppt.Ret_ship_noticeId;
import org.apache.commons.lang.StringUtils;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_ship_notice;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.dao.IRetShipNoticeDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.plan.service.IFdpaplydnService;
import com.dx.mes.facade.plan.tx.Fdpaplydn.FdpaplydnI;
import com.dx.mes.facade.plan.tx.Fdpaplydn.FdpaplydnIA;
import com.dx.mes.facade.plan.tx.Fdpaplydn.FdpaplydnO;
import com.dx.mes.facade.plan.tx.Fdpaplydn.FdpaplydnOA;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("fdpaplydnService")
@SuppressWarnings({"unchecked"})
public class Fdpaplydnservice implements IFdpaplydnService{
	private static final String TX_NAME = T_FDPAPLYDN;
	private LogUtils logUtils;
	
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IRetShipNoticeDao retShipNoticeDao;
	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(Fdpaplydnservice.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(Fdpaplydnservice.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		String strOutTrx="";
		FdpaplydnI inTrx  = new FdpaplydnI();
		FdpaplydnO outTrx = new FdpaplydnO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);	
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		
		try {
			inTrx = JacksonUtil.fromJson(strInTrx, FdpaplydnI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByConditionFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_ADD:
				addFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_UPDATE:
				updateFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_DELETE:
				deleteFnc(inTrx,outTrx);
				break;
			case 'C':
				closeFnc(inTrx,outTrx);
				break;
			case 'O':
				openFnc(inTrx,outTrx);
				break;
			case 'M':
				queryMdlFnc(inTrx,outTrx);
				break;
			/*case 'M':
				findMdlByDn(inTrx,outTrx);
				break;*/
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retWODao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}


	public void queryByConditionFnc(FdpaplydnI inTrx,FdpaplydnO outTrx) {
		List<FdpaplydnIA> iary=inTrx.getIary();
		List<FdpaplydnOA> oaryList = new ArrayList<FdpaplydnOA>();
		String hql2 = "FROM Ret_ship_notice where 1=1 ";
			String cusIdFk = iary.get(0).getCus_id_fk();
			String dnno = iary.get(0).getDn_no();
			String mdlIdFk = iary.get(0).getMdl_id_fk();
			String dnStat = iary.get(0).getDn_stat();

			if (cusIdFk != null && cusIdFk.length() != 0) {
				hql2 = hql2 + " and cus_id_fk='" + cusIdFk + "'";
	     	}
	     	if (dnno != null  && dnno.length() != 0) {
			    hql2 = hql2 + " and dn_no='" + dnno + "'";
		    }
			if (mdlIdFk != null && mdlIdFk.length() != 0) {
				hql2 = hql2 + " and mdl_id_fk='" + mdlIdFk + "'";
			}
		   /* if (plnshiptimestamp != null && plnshiptimestamp.length() != 0) {
			    hql2 = hql2 + "and pln_ship_timestamp like '%" + plnshiptimestamp +"%'";
		    }	*/
			/*if (dnStat != null && dnStat.length() != 0) {
				hql2 = hql2 + "and dn_stat='" + dnStat + "'";
			}*/

		List<Ret_ship_notice> ret_ship_notices = retShipNoticeDao.find(hql2);
		//HashSet 去重
		HashSet<String> hashSet = new HashSet<String>();
		for(int i = 0; i < ret_ship_notices.size(); i++){
			String dnNo = ret_ship_notices.get(i).getId().getDn_no();
			hashSet.add(dnNo);
		}

		Iterator<String> it = hashSet.iterator();
		while (it.hasNext()) {
			String dnNo = it.next();
			String hql = "FROM Ret_ship_notice where 1=1";
			hql = hql + " and dn_no = '" + dnNo + "'";
			List<Ret_ship_notice> retShipNotices = retShipNoticeDao.find(hql);

			FdpaplydnOA fdpaplydnOA = new FdpaplydnOA();
			fdpaplydnOA.setCus_id_fk(retShipNotices.get(0).getCus_id_fk());
			fdpaplydnOA.setDn_no(retShipNotices.get(0).getId().getDn_no());
			if(retShipNotices.get(0).getCrt_timestamp() != null){
				fdpaplydnOA.setCrt_timestamp(retShipNotices.get(0).getCrt_timestamp().toString());
			}
			fdpaplydnOA.setDn_stat(retShipNotices.get(0).getDn_stat());
			if(retShipNotices.get(0).getPln_ship_timestamp() != null){
				fdpaplydnOA.setPln_ship_timestamp(retShipNotices.get(0).getPln_ship_timestamp().toString());
			}
			Integer totalPlnShipQty = 0;
			for(int i = 0; i< retShipNotices.size();i++) {
				Integer plnShipQty = retShipNotices.get(i).getPln_ship_qty();
				totalPlnShipQty += plnShipQty;
			}
			//总计划交货数量
			fdpaplydnOA.setPln_ship_qtys(totalPlnShipQty);
			//实际交货时间
			if(retShipNotices.get(0).getAct_ship_timestamp() != null){
				fdpaplydnOA.setAct_ship_timestamp(retShipNotices.get(0).getAct_ship_timestamp().toString());
			}
			fdpaplydnOA.setWo_id_fk(retShipNotices.get(0).getWo_id_fk());//工单号
			fdpaplydnOA.setAct_ship_qty(retShipNotices.get(0).getAct_ship_qty());//已出货数量
			fdpaplydnOA.setShip_note(retShipNotices.get(0).getShip_note());//出货备注
			fdpaplydnOA.setFifoFlg(retShipNotices.get(0).getFifo_flg());//是否先进先出
			fdpaplydnOA.setShip_address(retShipNotices.get(0).getShip_address());
			oaryList.add(fdpaplydnOA);
		}

		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
	}

	public void queryMdlFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		FdpaplydnIA iary = inTrx.getIary().get(0);
		String dnNo = iary.getDn_no();
		String hql = "FROM Ret_ship_notice where 1=1";
		hql = hql + " and dn_no = '" + dnNo + "'";
		List<Ret_ship_notice> retShipNotices = retShipNoticeDao.find(hql);
		List<FdpaplydnOA> oaryList = new ArrayList<FdpaplydnOA>();
		for(int i = 0; i < retShipNotices.size(); i++){
			FdpaplydnOA oary = new FdpaplydnOA();
			String mdlIdFk = retShipNotices.get(i).getId().getMdl_id_fk();
			Integer plnShipQty = retShipNotices.get(i).getPln_ship_qty();
			oary.setMdl_id_fk(mdlIdFk);
			oary.setPln_ship_qty(plnShipQty);
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;

	}
	
	public void addFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String  evt_usr = inTrx.getEvt_usr();
		//String   evtSeqId = GUIDGenerator.javaGUID();
		List<FdpaplydnIA> iaryList=inTrx.getIary();
		//String dnno =iary.get(0).getDn_no();
		String dnno = generateCode();;
		List<Ret_ship_notice> ret_ship_notices = new ArrayList<Ret_ship_notice>();
		for(int i = 0 ; i < iaryList.size() ;i++){

			String cusIdFk=iaryList.get(0).getCus_id_fk();
			String mdlIdFk=iaryList.get(0).getMdl_id_fk();
			Integer plnShipQty=iaryList.get(0).getPln_ship_qty();
			String shipAddress=iaryList.get(0).getShip_address();
			String shipNote = iaryList.get(0).getShip_note();
			Timestamp plnShipTimestamp = iaryList.get(0).getPln_ship_timestamp();
			String fifoFlg = iaryList.get(0).getFifoFlg();

			Ret_ship_notice newRetShipNotice = new Ret_ship_notice();
			Ret_ship_noticeId ret_ship_noticeId = new Ret_ship_noticeId();
			ret_ship_noticeId.setDn_no(dnno);
			ret_ship_noticeId.setMdl_id_fk(mdlIdFk);
			newRetShipNotice.setId(ret_ship_noticeId);

			newRetShipNotice.setCus_id_fk(cusIdFk);
			newRetShipNotice.setPln_ship_qty(plnShipQty);
			newRetShipNotice.setShip_address(shipAddress);
			newRetShipNotice.setAct_ship_qty(0);
			newRetShipNotice.setShip_note(shipNote);
			newRetShipNotice.setPln_ship_timestamp(plnShipTimestamp);
			newRetShipNotice.setFifo_flg(fifoFlg);
			//自己添加的数据
			newRetShipNotice.setCrt_timestamp(current_timestamp);
			newRetShipNotice.setDn_stat("WAIT");
			newRetShipNotice.setEvt_usr(evt_usr);
			newRetShipNotice.setEvt_timestamp(Util.getCurrentTimestamp());
			newRetShipNotice.setEvt_cate(EVT_SHIP_NOTICE_ADD);
			ret_ship_notices.add(newRetShipNotice);
		}
		retShipNoticeDao.saveOrUpdateAll(ret_ship_notices);

		List<FdpaplydnOA> oary = new ArrayList<FdpaplydnOA>();
		FdpaplydnOA fdpaplydnOA = new FdpaplydnOA();
		fdpaplydnOA.setCus_id_fk(ret_ship_notices.get(0).getCus_id_fk());
		fdpaplydnOA.setDn_no(ret_ship_notices.get(0).getId().getDn_no());
		if(ret_ship_notices.get(0).getCrt_timestamp() != null){
			fdpaplydnOA.setCrt_timestamp(ret_ship_notices.get(0).getCrt_timestamp().toString());
		}
		fdpaplydnOA.setDn_stat(ret_ship_notices.get(0).getDn_stat());
		if(ret_ship_notices.get(0).getPln_ship_timestamp() != null){
			fdpaplydnOA.setPln_ship_timestamp(ret_ship_notices.get(0).getPln_ship_timestamp().toString());
		}
		Integer totalPlnShipQty = 0;
		for(int i = 0; i< ret_ship_notices.size();i++) {
				Integer plnShipQty = ret_ship_notices.get(i).getPln_ship_qty();
				totalPlnShipQty += plnShipQty;
		}
		//总计划交货数量
		fdpaplydnOA.setPln_ship_qtys(totalPlnShipQty);
		//实际交货时间
		if(ret_ship_notices.get(0).getAct_ship_timestamp() != null){
			fdpaplydnOA.setAct_ship_timestamp(ret_ship_notices.get(0).getAct_ship_timestamp().toString());
		}
		fdpaplydnOA.setWo_id_fk(ret_ship_notices.get(0).getWo_id_fk());//工单号
		fdpaplydnOA.setAct_ship_qty(ret_ship_notices.get(0).getAct_ship_qty());//已出货数量
		fdpaplydnOA.setShip_note(ret_ship_notices.get(0).getShip_note());//出货备注
		fdpaplydnOA.setFifoFlg(ret_ship_notices.get(0).getFifo_flg());//是否先进先出
		oary.add(fdpaplydnOA);
		outTrx.setOary(oary);
     	outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}


	public void errActionFnc(FdpaplydnO  outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}


	public void updateFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		String  evt_usr = inTrx.getEvt_usr();
		List<FdpaplydnIA> iaryList = inTrx.getIary();
		//String evtSeqId = iary.get(0).getEvt_seq_id();
		List<Ret_ship_notice> ret_ship_notices = new ArrayList<Ret_ship_notice>();

		//删除数据库有，但是产品列表里没有的
		String dnNo = iaryList.get(0).getDn_no();
		String hql = "FROM Ret_ship_notice where 1 = 1";
		hql = hql + " and dn_no = '" + dnNo + "'";
		List<Ret_ship_notice>  retShipNotices= retShipNoticeDao.find(hql);
		for(int i = 0;i < retShipNotices.size() ;i++) {
			//数据库里有的数据
			String mdlIdFk = retShipNotices.get(i).getId().getMdl_id_fk();
			for (int j = 0; j < iaryList.size(); j++) {
				//前台的数据
				if (iaryList.get(j).getMdl_id_fk().equals(mdlIdFk)) {
					break;
				} else {
					Ret_ship_noticeId id = new Ret_ship_noticeId();
					id.setMdl_id_fk(mdlIdFk);
					id.setDn_no(dnNo);
					Ret_ship_notice ret_ship_notice = retShipNoticeDao.get(id);
					retShipNoticeDao.delete(ret_ship_notice);
				}
			}
		}


		for(int i = 0 ; i < iaryList.size() ; i++){
			String dnno = iaryList.get(i).getDn_no();
			String cusIdFk = iaryList.get(i).getCus_id_fk();
			String mdlIdFk = iaryList.get(i).getMdl_id_fk();
			Integer plnShipQty = iaryList.get(i).getPln_ship_qty();
			Integer actShipQty = iaryList.get(i).getAct_ship_qty();
			String shipAddress = iaryList.get(i).getShip_address();
			String shipNote = iaryList.get(i).getShip_note();
			String fifoFlg = iaryList.get(i).getFifoFlg();
			Timestamp plnShipTimestamp = iaryList.get(i).getPln_ship_timestamp();

			Ret_ship_noticeId id = new Ret_ship_noticeId();
			if(dnno != null){
				id.setDn_no(dnno);
			}
			if( mdlIdFk != null){
				id.setMdl_id_fk(mdlIdFk);
			}
			Ret_ship_notice ret_ship_notice = retShipNoticeDao.get(id);
			if(ret_ship_notice == null){
				//新增
				Ret_ship_notice newRetShipNotice = new Ret_ship_notice();
				newRetShipNotice.setId(id);
				if (cusIdFk != null) {
					newRetShipNotice.setCus_id_fk(cusIdFk);
				}
				if (plnShipQty != null) {
					newRetShipNotice.setPln_ship_qty(plnShipQty);
				}
				if (actShipQty != null) {
					newRetShipNotice.setAct_ship_qty(actShipQty);
				}
				if (shipAddress != null) {
					newRetShipNotice.setShip_address(shipAddress);
				}
				if (shipNote != null) {
					newRetShipNotice.setShip_note(shipNote);
				}
				if (plnShipTimestamp != null) {
					newRetShipNotice.setPln_ship_timestamp(plnShipTimestamp);
				}
				if(fifoFlg != null){
					newRetShipNotice.setFifo_flg(fifoFlg);
				}
				newRetShipNotice.setCrt_timestamp(Util.getCurrentTimestamp());
				newRetShipNotice.setDn_stat("WAIT");
				newRetShipNotice.setEvt_usr(evt_usr);
				newRetShipNotice.setEvt_timestamp(Util.getCurrentTimestamp());
				newRetShipNotice.setEvt_cate(EVT_SHIP_NOTICE_ADD);
				retShipNoticeDao.save(newRetShipNotice);
				ret_ship_notices.add(newRetShipNotice);
			}else{
				//更新
				if (cusIdFk != null) {
					ret_ship_notice.setCus_id_fk(cusIdFk);
				}
				if (plnShipQty != null) {
					ret_ship_notice.setPln_ship_qty(plnShipQty);
				}
				if (actShipQty != null) {
					ret_ship_notice.setAct_ship_qty(actShipQty);
				}
				if (shipAddress != null) {
					ret_ship_notice.setShip_address(shipAddress);
				}
				if (shipNote != null) {
					ret_ship_notice.setShip_note(shipNote);
				}
				if (plnShipTimestamp != null) {
					ret_ship_notice.setPln_ship_timestamp(plnShipTimestamp);
				}
				if(fifoFlg != null){
					ret_ship_notice.setFifo_flg(fifoFlg);
				}
				ret_ship_notice.setEvt_usr(evt_usr);
				ret_ship_notice.setEvt_timestamp(Util.getCurrentTimestamp());
				ret_ship_notice.setEvt_cate(EVT_SHIP_NOTICE_UPDATE);
				retShipNoticeDao.update(ret_ship_notice);
				ret_ship_notices.add(ret_ship_notice);
			}
		}

		List<FdpaplydnOA> oary = new ArrayList<FdpaplydnOA>();
		FdpaplydnOA fdpaplydnOA = new FdpaplydnOA();
		fdpaplydnOA.setCus_id_fk(ret_ship_notices.get(0).getCus_id_fk());
		fdpaplydnOA.setDn_no(ret_ship_notices.get(0).getId().getDn_no());
		if(ret_ship_notices.get(0).getCrt_timestamp() != null){
			fdpaplydnOA.setCrt_timestamp(ret_ship_notices.get(0).getCrt_timestamp().toString());
		}
		fdpaplydnOA.setDn_stat(ret_ship_notices.get(0).getDn_stat());
		if(ret_ship_notices.get(0).getPln_ship_timestamp() != null){
			fdpaplydnOA.setPln_ship_timestamp(ret_ship_notices.get(0).getPln_ship_timestamp().toString());
		}
		Integer totalPlnShipQty = 0;
		for(int i = 0; i< ret_ship_notices.size();i++) {
			Integer plnShipQty = ret_ship_notices.get(i).getPln_ship_qty();
			totalPlnShipQty += plnShipQty;
		}
		//总计划交货数量
		fdpaplydnOA.setPln_ship_qtys(totalPlnShipQty);
		//实际交货时间
		if(ret_ship_notices.get(0).getAct_ship_timestamp() != null){
			fdpaplydnOA.setAct_ship_timestamp(ret_ship_notices.get(0).getAct_ship_timestamp().toString());
		}
		fdpaplydnOA.setWo_id_fk(ret_ship_notices.get(0).getWo_id_fk());//工单号
		fdpaplydnOA.setAct_ship_qty(ret_ship_notices.get(0).getAct_ship_qty());//已出货数量
		fdpaplydnOA.setShip_note(ret_ship_notices.get(0).getShip_note());//出货备注
		fdpaplydnOA.setFifoFlg(ret_ship_notices.get(0).getFifo_flg());//是否先进先出
		oary.add(fdpaplydnOA);
		outTrx.setOary(oary);
     	outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}


	public void deleteFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		FdpaplydnIA iaryA = inTrx.getIary().get(0);
		String dnNo  = iaryA.getDn_no();
		//String evtSeqId = iaryA.getEvt_seq_id();
		String hql = "From Ret_ship_notice where 1=1";
		hql = hql + " and dn_no = '" + dnNo +"'";
		List<Ret_ship_notice> ret_ship_notices = retShipNoticeDao.find(hql);
		if(ret_ship_notices.size() == 0) {
			outTrx.setRtn_code(E_FDPAPLYDN_NOT_FOUND + "");
			outTrx.setRtn_mesg("交货订单不存在");
			return;
		}
		retShipNoticeDao.deleteAll(ret_ship_notices);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}


	public void closeFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		FdpaplydnIA iaryA = inTrx.getIary().get(0);
		//String evtSeqId = iaryA.getEvt_seq_id();
		String dnNo = iaryA.getDn_no();
		String hql = "From Ret_ship_notice where 1=1";
		hql = hql + " and dn_no = '" + dnNo +"'";
		List<Ret_ship_notice> ret_ship_notices = retShipNoticeDao.find(hql);

		if (ret_ship_notices.size() == 0) {
			outTrx.setRtn_code(E_FDPAPLYDN_NOT_FOUND + "");
			outTrx.setRtn_mesg("交货订单不存在");
			return;
		}

		for (int i = 0 ; i < ret_ship_notices.size(); i++){
			Ret_ship_notice ret_ship_notice = ret_ship_notices.get(i);
			ret_ship_notice.setDn_stat("CLOSE");
		}
		retShipNoticeDao.saveOrUpdateAll(ret_ship_notices);

		List<FdpaplydnOA> oary = new ArrayList<FdpaplydnOA>();

		FdpaplydnOA fdpaplydnOA = new FdpaplydnOA();
		fdpaplydnOA.setDn_no(ret_ship_notices.get(0).getId().getDn_no());
		fdpaplydnOA.setCus_id_fk(ret_ship_notices.get(0).getCus_id_fk());
		//fdpaplydnOA.setMdl_id_fk(ret_ship_notice.getMdl_id_fk());
		//fdpaplydnOA.setWo_id_fk(ret_ship_notice.getWo_id_fk());
		fdpaplydnOA.setAct_ship_qty(ret_ship_notices.get(0).getAct_ship_qty());
		fdpaplydnOA.setPln_ship_qty(ret_ship_notices.get(0).getPln_ship_qty());
		fdpaplydnOA.setShip_address(ret_ship_notices.get(0).getShip_address());
		fdpaplydnOA.setShip_note(ret_ship_notices.get(0).getShip_note());
		fdpaplydnOA.setPln_ship_timestamp(ret_ship_notices.get(0).getPln_ship_timestamp().toString());
		//fdpaplydnOA.setEvt_seq_id(ret_ship_notices.get(0).getEvt_seq_id());
		fdpaplydnOA.setDn_stat(ret_ship_notices.get(0).getDn_stat());
		fdpaplydnOA.setCrt_timestamp(ret_ship_notices.get(0).getCrt_timestamp().toString());
		oary.add(fdpaplydnOA);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}


	public void openFnc(FdpaplydnI inTrx,FdpaplydnO outTrx){
		FdpaplydnIA iaryA = inTrx.getIary().get(0);
		//String evtSeqId = iaryA.getEvt_seq_id();
		String dnNo = iaryA.getDn_no();
		String hql = "From Ret_ship_notice where 1=1";
		hql = hql + " and dn_no = '" + dnNo +"'";
		List<Ret_ship_notice> ret_ship_notices = retShipNoticeDao.find(hql);

		if (ret_ship_notices.size() == 0) {
			outTrx.setRtn_code(E_FDPAPLYDN_NOT_FOUND + "");
			outTrx.setRtn_mesg("交货订单不存在");
			return;
		}

		for(int i = 0 ; i < ret_ship_notices.size(); i++){
			ret_ship_notices.get(i).setDn_stat("WAIT");
		}
		retShipNoticeDao.saveOrUpdateAll(ret_ship_notices);

		List<FdpaplydnOA> oary = new ArrayList<FdpaplydnOA>();
		FdpaplydnOA fdpaplydnOA = new FdpaplydnOA();
		fdpaplydnOA.setDn_no(ret_ship_notices.get(0).getId().getDn_no());
		fdpaplydnOA.setCus_id_fk(ret_ship_notices.get(0).getCus_id_fk());
		//fdpaplydnOA.setMdl_id_fk(ret_ship_notice.getMdl_id_fk());
		//fdpaplydnOA.setWo_id_fk(ret_ship_notice.getWo_id_fk());
		fdpaplydnOA.setAct_ship_qty(ret_ship_notices.get(0).getAct_ship_qty());
		fdpaplydnOA.setPln_ship_qty(ret_ship_notices.get(0).getPln_ship_qty());
		fdpaplydnOA.setShip_address(ret_ship_notices.get(0).getShip_address());
		fdpaplydnOA.setShip_note(ret_ship_notices.get(0).getShip_note());
		fdpaplydnOA.setPln_ship_timestamp(ret_ship_notices.get(0).getPln_ship_timestamp().toString());
		//fdpaplydnOA.setEvt_seq_id(ret_ship_notices.get(0).getEvt_seq_id());
		fdpaplydnOA.setDn_stat(ret_ship_notices.get(0).getDn_stat());
		fdpaplydnOA.setCrt_timestamp(ret_ship_notices.get(0).getCrt_timestamp().toString());
		oary.add(fdpaplydnOA);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	/*//根据DN号找出产品，去重
	public void findMdlByDn(FdpaplydnI inTrx,FdpaplydnO outTrx){
		FdpaplydnIA iary = inTrx.getIary().get(0);
		String DnNo = iary.getDn_no();
		String hql = "From Ret_ship_notice where dn_no ='" + DnNo + "'";
		List<Ret_ship_notice> ret_ship_notices = retShipNoticeDao.find(hql);
		Set<String> mdlIds = new HashSet<String>();
		for (Ret_ship_notice rsn : ret_ship_notices) {
			mdlIds.add(rsn.getMdl_id_fk());
		}
		
		Iterator<String> it = mdlIds.iterator();
		List<FdpaplydnOA> oaryList = new ArrayList<FdpaplydnOA>();
		while(it.hasNext()){
			String mdlIdFk = it.next();
			if(mdlIdFk == null){
				outTrx.setRtn_code(E_FDPAPLYSO_NOT_FOUND + "");
				outTrx.setRtn_mesg("该交货订单没有对应的产品信息，请重新选择！");
				return;
			}
				FdpaplydnOA oary = new FdpaplydnOA();
				oary.setMdl_id_fk(mdlIdFk);
				oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}*/


	public String generateCode(){
		String code ="";
		int i =0;
		char[] Digit = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J', 'K', 'L', 'M', 'N','O','P', 'Q', 'R', 'S',
				'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
		Timestamp currentTime= Util.getCurrentTimestamp();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sf.format(currentTime);
		String timeCode = String.join("",time.split("-")).substring(2);
		String likeCode = "W"+timeCode+"A";
		String hql = "From Ret_ship_notice where dn_no like '%" +likeCode+"%'";
		List<Ret_ship_notice> Ret_ship_notices = retShipNoticeDao.find(hql);

		if(Ret_ship_notices.size() > 0){
			List<Byte> bytes = new ArrayList<Byte>();
			for (Ret_ship_notice ret_ship_notice : Ret_ship_notices) {
				String dnno = ret_ship_notice.getId().getDn_no();
				char ch = dnno.charAt(dnno.length()-1);
				byte b =(byte)ch;
				bytes.add(b);
			}
			byte mByte= Collections.max(bytes);
			char mch = (char)mByte;
			if(mch == Digit[8]){
				mByte = 65;
				mch =(char)mByte;
				code = likeCode+mch;
				return code;
			}
			mByte++;
			mch =(char)mByte;
			code = likeCode+mch;
			return code;
		}else{
			i = 0;
			code = likeCode + Digit[i];
			return code;
		}
	}
}
